package com.szosen.util;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.szosen.jkzj.R;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * 显示四列文本的适配器，设置第一行为标题行，背景与内容行不同
 */
public class FourCloumAdpter extends BaseAdapter {
    private static final int LENGHT = 5;
    private List<String[]> datas;
    private Context context;
    private LinkedHashMap<String, String> map;
    //可以勾选的最大数量
    private static final int maxCount = 1000;
    Handler mHandler = null;
    /***
     * 刷新显示已选择条目个数what
     */
    public static final int what_refresh_data = 0x4;

    public FourCloumAdpter(Context context, List<String[]> datas) {
        this.context = context;
        this.datas = datas;
        map = new LinkedHashMap<String, String>();
    }

    public FourCloumAdpter(Context context, List<String[]> datas, Handler handler) {
        this.context = context;
        this.datas = datas;
        map = new LinkedHashMap<String, String>();
        this.mHandler = handler;
    }

    public LinkedHashMap<String, String> getMapData() {
        return map;
    }

    public synchronized boolean putData(String position, String data) {
        if (map != null && map.size() < maxCount) {
            map.put(position, data);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置所有的数据都勾选
     */
    public void putAllDataSelected(boolean isAllSelect) {
        if (isAllSelect) {
            for (int i = 0; i < datas.size(); i++) {
                String idcard = datas.get(i)[2];
                map.put(i + "", idcard);
            }
        } else {
            map.clear();
        }
        this.notifyDataSetChanged();
    }


    public boolean removeData(String position) {
        String s = map.remove(position);
        if (!TextUtils.isEmpty(s)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //items的数据0为姓名 1 为年龄 2为身份证号 3为性别 4 time 5 序号
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holder holder;
        // 只有为空时才构造
        if (convertView == null) {
            holder = new Holder();
            holder.dataET = new TextView[LENGHT];
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.fourcloum_item_list, null);
            holder.dataET[0] = (TextView) convertView
                    .findViewById(R.id.list_data_1);
            holder.dataET[1] = (TextView) convertView
                    .findViewById(R.id.list_data_2);
            holder.dataET[2] = (TextView) convertView
                    .findViewById(R.id.list_data_3);
            holder.dataET[3] = (TextView) convertView
                    .findViewById(R.id.list_data_4);
            holder.layout = (LinearLayout) convertView
                    .findViewById(R.id.record_layout);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.list_cb);
            holder.data_Number = (TextView) convertView
                    .findViewById(R.id.list_data_serialNumber);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        if (position < datas.size()) {
            final String[] line = datas.get(position);
            holder.checkBox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                                     boolean isChecked) {
                            //根据time数据库唯一键来查询、标记
                            String time = line[4];
                            if (isChecked) {
                                boolean b = putData(position + "", time);
                                if (!b) {
                                    holder.checkBox.setChecked(false);
                                }
                            } else {
                                removeData(position + "");
                            }
                            mHandler.obtainMessage(what_refresh_data).sendToTarget();
                        }
                    });
            String itemData = map.get(position + "");
            //查询记录checkbox的map，如果不为空，证明是有选择的。然后勾选。
            if (!TextUtils.isEmpty(itemData)) {
                holder.checkBox.setChecked(true);
            } else {
                holder.checkBox.setChecked(false);
            }
            for (int i = 0; i < LENGHT; i++) {
                if (line[i] == null) {
                    continue;
                }
                String item;
                if (line[i].length() > 200)// 显示心电太长了,只显示部分
                    item = line[i].substring(0, 20) + "...";
                else
                    item = line[i];
                if (holder.dataET[i] != null) {
                    holder.dataET[i].setText(item);
                }
            }
            holder.data_Number.setText(line[5]);

        }
        //	holder.layout.setBackgroundResource(R.drawable.button_selector);
    /*	if (position == 0) {
            holder.layout.setBackgroundResource(R.color.record_title);
		} else {
			// 设置内容栏的背景
			holder.layout.setBackgroundResource(R.drawable.button_selector);
		}*/
        return convertView;
    }

    /***
     * 用于暂存数据
     *
     */
    class Holder {
        TextView[] dataET;// 显示的内容
        LinearLayout layout;// 设定背景
        CheckBox checkBox;//用于选择
        TextView data_Number;
    }
}
